/*********************************************************************************
 *      Copyright:  (C) 2020 Juan
 *                  All rights reserved.
 *
 *       Filename:  Connect_receive.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(22/07/20)
 *         Author:  wangruijie <1046299465@qq.com>
 *      ChangeLog:  1, Release initial version on "22/07/20 04:05:14"
 *                 
 ********************************************************************************/
#include"G4module_server.h"

/* *******************************************
 * 函数名：on_accept
 * 功能：当有新连接连到server时，就通过libevent调用此函数，连接客户端，每个连接对应一个新线程
 * 参数：sock，event，arg
 * 返回值：
 * *******************************************/
void on_accept(int sock, short event, void* arg)
{
    struct sockaddr_in     remote_addr;
    int                    sin_size =  sizeof(struct sockaddr_in);
    int                    new_fd;

    new_fd = accept(sock,  (struct sockaddr*) &remote_addr, (socklen_t*)&sin_size);
    if(new_fd < 0)
    {
        printf("Accept error in on_accept()");
        return ;
    }
    printf("The accept new_fd = %d\n", new_fd);

    /* 调用accept_new_thread()函数创建子线程 */
    accept_new_thread(new_fd);     

    printf("on_accept() finished for fd = %d\n", new_fd);

}

/* *************************************************
 * 函数名：on_read
 * 功能：接收客户端的数据
 * 参数：sock，event，arg
 * 返回值：
 * *************************************************/
void on_read(int sock, short event, void* arg)
{
    struct sock_ev          *event_struct;
    int                     rv;
    char                    buffer[BUF_SIZE];
    struct sock_ev_write    *sock_ev_write_struct;
    struct event            *write_ev;

    if(NULL == arg)
    {
        return ;
    }

    event_struct = (struct sock_ev*) arg;     //获取传进来的参数
    memset(buffer, 0, sizeof(char)*BUF_SIZE);     //清零buf
    rv = read(sock, buffer, BUF_SIZE);
    if(rv < 0)
    {
        printf("read error:%s\n", strerror(errno));
        return  ;
    }
    else if(0 == rv)
    {
        destroy_sock_ev(event_struct);
        close(sock);
        return;
    }
    printf("receive data:%s\n", buffer);

    /* 为该结构体malloc一段内存 */
    sock_ev_write_struct = (struct sock_ev_write*)malloc(sizeof(struct sock_ev_write));
    /* 把读到的内容赋值到结构体里的buf中 */
    sock_ev_write_struct->buffer = buffer;
    /* 为该结构体malloc一段内存 */
    write_ev = (struct event*)malloc(sizeof(struct event));   //发生写事件（也就是只要socket缓冲区可写）时，就将反馈数据通过socket写回客户端
    sock_ev_write_struct->write_ev = write_ev;
    /* 把写的回调函数加入even_base中 */
    event_set(write_ev, sock, EV_WRITE, on_write, sock_ev_write_struct);
    event_base_set(event_struct->base, write_ev);
    event_add(write_ev, NULL);

    printf("on_read() finished, sock = %d\n", sock);
}

/* ****************************************
 * 函数名：on_write()
 * 功能：发送数据给客户端
 * 参数：sock, event, arg
 * 返回值：
 * ****************************************/
void on_write(int sock, short event, void* arg)
{
    struct sock_ev_write      *sock_ev_write_struct;
    char                      buffer[BUF_SIZE];

    printf("on_write() called, sock = %d\n", sock);

    if(NULL == arg)
    {
        printf("Error! void* arg is NULL in on_write()\n");
        return;
    }
    
    sock_ev_write_struct = (struct sock_ev_write*)arg;
    sprintf(buffer, "fd=%d, received[%s]", sock, sock_ev_write_struct->buffer);
    int write_num = write(sock, buffer, strlen(buffer));  //发送数据

    /* 销毁结构体 */
    destroy_sock_ev_write(sock_ev_write_struct);
    printf("on_write() finished, sock=%d\n", sock);
}
